package top.kaoshanji.ch03;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Scanner;

/**
 * 代码清单 ch03-04：do/while示例(计算养老金2)
 * author kaoshanji
 * ^_^
 * create time 2024/11/23 18:34
 */
public class Retirement2 {

    private static final Logger logger = LogManager.getLogger(Retirement2.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch03-04：do/while示例(计算养老金2)...");

        Scanner in = new Scanner(System.in);

        System.out.print("How much money will you contribute every year? ");
        // 每天存入的数额
        double payment = in.nextDouble();

        System.out.print("Interest rate in %: ");
        // 利息
        double interestRate = in.nextDouble();

        // 银行余额
        double balance = 0;
        int year = 0;

        String input;


        do {
            // add this year's payment and interest
            balance += payment;
            double interest = balance * interestRate / 100;
            balance += interest;

            year++;

            // print current balance
            System.out.printf("After year %d, your balance is %,.2f%n", year, balance);

            // ask if ready to retire and get input
            System.out.print("Ready to retire? (Y/N) ");
            input = in.next();
        } while (input.equals("N"));


    }

}
